package com.weixing.mall.provider.controller.admin;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.dto.SpecDto;
import com.weixing.mall.provider.model.query.SpecQuery;
import com.weixing.mall.provider.service.ISpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @ClassName SpecController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/9
 * @Version 1.0
 **/
@RestController
@RequestMapping("spec")
@Api(value = "商品规格APi", tags = "商品规格")
public class SpecController extends BaseController {

    @Autowired
    private ISpecService specService;


    @ApiOperation(value = "条件获取商品规格")
    @GetMapping("getList")
    public Result getList(SpecQuery specQuery) {
        return ResultUtil.success(specService.selectList(specQuery));
    }


    @ApiOperation(value = "保存规格")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated SpecDto specDto) {
        specService.save(specDto);
        return ResultUtil.success();
    }

    /**
     * @Description 只删除没有关联的规格 <br/>
     * @author gjq
     * @date 2019/8/12 11:19
     * @param id
     * @return com.weixing.mall.base.wrapper.Result
     */
    @ApiOperation(value = "删除规格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "规格id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result delById(@PathVariable long id) {
        return specService.removeById(id)  ? ResultUtil.success() : ResultUtil.error();
    }
    @ApiOperation(value = "图片库上传图片 flag  为0 根据文件名称，新建目录。为1时，不新建目录")
    @PostMapping("/init")
    public Result upload(@RequestParam("file") MultipartFile file){
        specService.init(file);
        return ResultUtil.success();
    }
}
